﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Multiplication : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Multiplication"/> class.
        /// </summary>
        public Multiplication()
        {
            this.ClipColor = true;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Multiplication"/> class.
        /// </summary>
        /// <param name="_multiplication">The _multiplication.</param>
        internal Multiplication(Multiplication _multiplication)
        {
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            return Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
        }

        /// <summary>
        /// Processes the specified bmpimg.
        /// </summary>
        /// <param name="_bitmap1">The _bitmap1.</param>
        /// <param name="_bitmap2">The _bitmap2.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;

            int nRMul = 0;
            int nGMul = 0;
            int nBMul = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    color2 = _bitmap2.GetPixel(x, y);

                    nRMul = color1.R * color2.R;
                    if (m_bClipColor) nRMul = Math.Min(nRMul, 255);
                    
                    nGMul = color1.G * color2.G;
                    if (m_bClipColor) nGMul = Math.Min(nGMul, 255);

                    nBMul = color1.B * color2.B;
                    if (m_bClipColor) nBMul = Math.Min(nBMul, 255);
                    bitmap.SetPixel(x, y, Color.FromArgb(nRMul, nGMul, nBMul));
                }
            }
            return bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <param name="_nConstant"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap, double _dConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    color = Color.FromArgb((int)(color.R * _dConstant), (int)(color.G * _dConstant), (int)(color.B * _dConstant));
                    _bitmap.SetPixel(x, y, color);
                }
            }
            return _bitmap;
        }

        #endregion

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Multiplication(this);
        }
    }
}
